package com.example.kotlindemo.activity

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.provider.Settings.Global.putInt
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.core.content.edit
import com.example.kotlindemo.R
import com.example.kotlindemo.base.BaseActivity
import com.example.kotlindemo.kotlinBean
import com.example.kotlindemo.kuozhanhanshu.toast
import com.example.kotlindemo.title_utils.Danli
import kotlinx.android.synthetic.main.activity_main.*
import java.util.prefs.Preferences

class MainActivity : BaseActivity(), View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        getPreferences(Context.MODE_PRIVATE).edit {
            putInt("10", 19)
        }
        getPreferences(Context.MODE_PRIVATE).madef {
            putInt("10", 19)
        }

        bt_01.setOnClickListener(this)
        bt_02.setOnClickListener(this)
        bt_03.setOnClickListener(this)
        bt_04.setOnClickListener(this)
        bt_05.setOnClickListener(this)
        /**
         * 这样的写法必须是一个java的方法
         * java单抽象方法
         *
         */
        bt_01.setOnClickListener {

        }



        "hhhhh".capitalize()//返回一个字符串的首字母大写


        Log.d("MainActivity", "onCreate")
    }


    /**
     * 这里需要注意 如果一个函数不返回任何有用的值，
     * 它的返回类型是 Unit。Unit 是一种只有一个值——Unit 的类型。这个值不需要显式返回：
     *
     */
    fun SharedPreferences.madef(black: SharedPreferences.Editor.() -> Unit) {
        val edit = edit()
        edit.black()
        edit.apply()

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.bt_01 -> {
                Log.d("taskId", "MainActivity$taskId")
                val intent = Intent(this, SecondActivity::class.java).apply {
                    putExtra("name", "liming")
                    putExtra("sex", "男")
                }
//                dosmosing()
//                startActivity(intent)//无返回参数的调用
                SecondActivity.show(
                    this,
                    "liming",
                    "男"
                )
                this@MainActivity.toast("JJJJ")
//                startActivityForRes  ult(intent, 1)//有返回值的调用
            }

            R.id.bt_02 -> {
                AlertDialog.Builder(this).apply {
                    setTitle("标题")
                    setMessage("这是内容")
                    setCancelable(false)
                    setPositiveButton("OK") { dialog, which ->
                        Toast.makeText(this@MainActivity, "OK", Toast.LENGTH_LONG).show()

                    }
                    setNegativeButton("Cancele") { dialog, which ->
                        Toast.makeText(this@MainActivity, "Cancele", Toast.LENGTH_LONG).show()
                    }.show()
                }
            }

            R.id.bt_03 -> {


                ListViewActivity.show(this@MainActivity)
            }
            R.id.bt_04 -> {
                RecyclerViewActivity.show(this@MainActivity)
            }
            R.id.bt_05 -> {
                MessageActivity.show(this@MainActivity)
            }
        }
    }

    override fun onStart() {
        super.onStart()
        Log.d("MainActivity", "onStart")

    }

    override fun onResume() {
        super.onResume()
        Log.d("MainActivity", "onResume")

    }

    override fun onPause() {
        super.onPause()
        Log.d("MainActivity", "onPause")

    }

    override fun onStop() {
        super.onStop()
        Log.d("MainActivity", "onStop")

    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("MainActivity", "onDestroy")

    }

    override fun onRestart() {
        super.onRestart()
        Log.d("MainActivity", "onRestart")

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            1 -> if (resultCode == Activity.RESULT_OK) {
                Toast.makeText(
                    this,
                    data?.getStringExtra("result"),
                    Toast.LENGTH_LONG
                ).show()
            }
        }
    }
}
